<template>
	<view class="merchant">
		<view class="head" :style="{height: pHeight + tHeight + 'px'}">
			<view class="xztitle" :style="[{height: pHeight + 'px', top: tHeight + 'px',lineHeight: pHeight + 'px'}]">
				{{headtitle}}
				<view class="iconfont icon-dayuhao1" @click="fanhui"></view>
			</view>
		</view>
		<view class="headtop" :style="{marginTop: pHeight + tHeight + 'px'}"></view>
		<view class="upload">
			<image :src="baseUrl + '/wximage/shangchuanbj.png'" @click="chooseImage" v-if="formData.sitepicture==''" class="bj"></image>
			<image :src="formData.sitepicture" @click="chooseImage" v-else class="bj"></image>
		</view>
		<view class="form">
			<view class="content clearfix">
				<view class="name fl">商品名称</view>
				<view class="inp fr">
					<input type="text" v-model="formData.name" placeholder="请输入商品名称" disabled />
				</view>
			</view>
			<view class="content clearfix">
				<view class="name fl">商品售价</view>
				<view class="inp fr">
					<input type="text" v-model="formData.danjia" placeholder="请输入商品售价" />
				</view>
			</view>
			<view class="content clearfix">
				<view class="name fl">成本价</view>
				<view class="inp fr">
					<input type="text" v-model="formData.costprice" placeholder="请输入成本价" />
				</view>
			</view>
			<view class="content contentlast clearfix">
				<view class="name fl">仓位</view>
				<view class="danxuan fr">
					<u-radio-group v-model="formData.positions">
						<u-radio name="1" @change="radioChange(1)" active-color="#368BF8">
							左仓
						</u-radio>
						<u-radio name="2" @change="radioChange(2)" active-color="#368BF8">
							右仓
						</u-radio>
					</u-radio-group>
				</view>
			</view>
		</view>
		<view class="footer">
			<view class="shanchu" @click="del">删除</view>
			<view class="submit" @click="submit">保存</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				baseUrl: getApp().globalData.baseUrl,
				headerTop: 0,
				pHeight: 0,
				tHeight: 0,
				formData:{
					agent_id: '',
					role_id: '',
					id: '',
					name: '',
					sitepicture: '',
					danjia: '',
					costprice: '',
					positions: 1,
				},
				headtitle: '新增商品',
			}
		},
		onLoad(options) {
			// 头部距离
			let res = uni.getSystemInfoSync() ///微信api方法
			let titleH;
			if (res && res['system']) {
				// 判断是否是安卓操作系统 （标题栏苹果为44px,安卓为48px）
				if (res['system'].indexOf('Android') > 0) {
					titleH = 48
				} else {
					titleH = 46
				}
				this.headerTop = titleH + res['statusBarHeight']
				this.pHeight = titleH
				this.tHeight = res['statusBarHeight']
			}
			
			this.formData.agent_id = uni.getStorageSync('agent_id');
			this.formData.role_id = uni.getStorageSync('role_id');
			
			if(options.goodscharger_id){
				this.formData.goodscharger_id = options.goodscharger_id;
				this.headtitle = '商品编辑';
				
				this.request({
					url: '/xcx/goodsdetail1',
					data: this.formData,
					method: "POST"
				}).then(res => {
					if (res.data.code == 200) {
						this.formData.name = res.data.data.name;
						this.formData.sitepicture = res.data.data.sitepicture;
						this.formData.danjia = res.data.data.danjia;
						this.formData.costprice = res.data.data.costprice;
						this.formData.positions = res.data.data.positions;
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						})
					}
				});
			}
		},
		methods: {
			del(){
				this.request({
					url: '/xcx/delgoods',
					data: this.formData,
					method: "POST"
				}).then(res => {
					if (res.data.code == 200) {
						uni.showToast({
							title: '成功',
							icon: 'none'
						})
						
						setTimeout(function(){
							uni.navigateBack()
						},2000)
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						})
					}
				});
			},
			submit(){
				if(this.formData.sitepicture==null||this.formData.sitepicture==''||this.formData.sitepicture==undefined){
					uni.showToast({
						title: '商品图片不能为空',
						icon: 'none'
					})
					return false;
				}
				if(this.formData.name==null||this.formData.name==''||this.formData.name==undefined){
					uni.showToast({
						title: '商品名称不能为空',
						icon: 'none'
					})
					return false;
				}
				if(this.formData.danjia==null||this.formData.danjia==''||this.formData.danjia==undefined){
					uni.showToast({
						title: '商品售价不能为空',
						icon: 'none'
					})
					return false;
				}
				if(this.formData.costprice==null||this.formData.costprice==''||this.formData.costprice==undefined){
					uni.showToast({
						title: '成本价不能为空',
						icon: 'none'
					})
					return false;
				}
				
				this.request({
					url: '/xcx/editgoods',
					data: this.formData,
					method: "POST"
				}).then(res => {
					if (res.data.code == 200) {
						uni.showToast({
							title: '成功',
							icon: 'none'
						})
						
						setTimeout(function(){
							uni.navigateBack()
						},2000)
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						})
					}
				});
			},
			topage(url){
				uni.navigateTo({
					url
				})
			},
			fanhui() {
				uni.navigateBack();
			},
			chooseImage() {
				let that = this
				uni.chooseImage({
					count: 1,
					sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
					success: function(res) {
						uni.uploadFile({
							url: 'https://aapi.jixiaodian.cn/api/common/cosupdate',
							filePath: res.tempFilePaths[0],
							name: 'file',
							success: function(res1) {
								let arr = JSON.parse(res1.data);
								that.formData.sitepicture = arr.data.path;
							},
							fail: function(res1) {
			
							},
						})
					}
				})
			},
			confirmlx(e) {
				this.jsvalue = e[0].value;
				this.lxlabel = e[0].label;
				this.formData.store_type = e[0].value;
			},
			radioChange(e) {
				this.formData.positions = e;
			},
		}
	}
</script>

<style lang="scss" scoped>
	.merchant {
		width: 100%;
		margin: 0 auto;
		height: 100vh;
		background: #f9f9ff;
		overflow-y: scroll;

		.head {
			width: 100%;
			position: fixed;
			top: 0;
			left: 0;
			z-index: 99;
			background: linear-gradient(90deg, #f9f9ff, #d4e8ff);

			.xztitle {
				position: fixed;
				left: 0%;
				top: 0;
				width: 100%;
				text-align: left;
				color: #333333;
				font-size: 32rpx;
				padding-left: 74rpx;
				font-weight: 700;

				text {
					font-size: 24rpx;
				}

				.iconfont {
					position: absolute;
					top: 0;
					left: 30rpx;
					font-size: 44rpx;
				}
			}
		}

		.headtop {
			width: 100%;		
			background: linear-gradient(90deg, #f9f9ff, #d4e8ff);
			height: 300rpx;
		}
		
		.upload{
			width: 94%;
			margin: 0 auto;
			margin-top: -280rpx;
			text-align: center;
			position: relative;
			
			.bj{
				width: 382rpx;
				height: 260rpx;
			}
			
			.sc{
				width: 100rpx;
				height: 100rpx;
				position: absolute;
				top: 56rpx;
				left: 140rpx;
			}
			
		}
		
		.form{
			width: 94%;
			margin: 0 auto;
			margin-top: 20rpx;
			background: #FFFFFF;
			border-radius: 20rpx;
			padding: 0 20rpx;
			box-shadow:rgba(223, 230, 255, 0.67) 0px 0px 6px;
			
			.content{
				height: 92rpx;
				line-height: 92rpx;
				border-bottom: 1rpx solid #D8D8D8;
				
				.name{
					font-size: 28rpx;
					color: #333333;
					width: 40%;
				}
				
				.inp{
					width: 60%;
					text-align: right;
					
					input{
						font-size: 28rpx;
						color: #999999;
						height: 92rpx;
						line-height: 92rpx;
					}
				}
				
				.xuanze{
					width: 60%;
					text-align: right;
					height: 92rpx;
					line-height: 92rpx;
					color: #368BF8;
				}
				
				.img{
					width: 60%;
					text-align: right;
					height: 92rpx;
					line-height: 92rpx;
					
					image{
						width: 108rpx;
						height: 64rpx;
						margin-top: 12rpx;
					}
				}
				
				.danxuan{
					display: flex;
					align-items: center;
					height: 92rpx;
				}
			}
			
			.contentlast{
				border: none;
			}
		}
		
		.shoufei{
			width: 94%;
			margin: 0 auto;
			margin-top: 40rpx;
			
			.headtitles{
				width: 100%;
				text-align: left;
				font-size: 32rpx;
				color: #333333;
				font-weight: 700;
				margin-bottom: 20rpx;
			}
			
			.fangan{
				width: 100%;
				margin: 0 auto;
				margin-bottom: 40rpx;
				background: #FFFFFF;
				border-radius: 20rpx;
				padding: 32rpx 20rpx 22rpx 20rpx;
				
				.top{
					padding-bottom: 20rpx;
					border-bottom: 1rpx solid #D8D8D8;
					
					.title{
						font-size: 28rpx;
						color: #333333;
					}
					
					.edit{
						font-size: 28rpx;
						color: #368BF8;
					}
				}
				
				.bottom{
					
					.text{
						font-size: 24rpx;
						color: #333333;
						margin-top: 20rpx;
					}
				}
			}
		}
		
		.footer{
			width: 100%;
			margin: 0 auto 40rpx;
			margin-top: 60rpx;
			height: 72rpx;
			text-align: center;
			
			.shanchu{
				float: left;
				width: 240rpx;
				height: 72rpx;
				line-height: 72rpx;
				background: #f0f0f0;
				margin-left: 100rpx;
				border-radius: 36rpx;
				border-top-left-radius: 0;
				border-bottom-left-radius: 42rpx;
				color: #000;
				font-size: 32rpx;
			}
			
			.submit{
				float: right;
				width: 240rpx;
				height: 72rpx;
				line-height: 72rpx;
				background: linear-gradient(90deg, #65bdfc, #398ef8);
				margin-right: 100rpx;
				border-radius: 36rpx;
				border-top-left-radius: 0;
				border-bottom-left-radius: 42rpx;
				color: #FFFFFF;
				font-size: 32rpx;
			}
		}
	}
</style>